package com.easy.frame.servers.sys.controller.dept;

import com.easy.frame.base.basic.ServiceResponse;
import com.easy.frame.base.basic.exception.ServiceException;
import com.easy.frame.base.basic.msg.ExceptionMsg;
import com.easy.frame.base.basic.pojo.vo.BaseTreeVO;
import com.easy.frame.base.service.base.BaseController;
import com.easy.frame.servers.sys.entity.dept.SysDept;
import com.easy.frame.servers.sys.service.dept.SysDeptService;
import com.easy.frame.servers.sys.vo.SysDeptVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 区域表相关接口类
 * *
 * * @author tp
 * * @date 2021/06/08 15:18
 */
@Slf4j
@Api(tags = "组织架构/sys-dept")
@RestController
@RequestMapping("/sys-dept")
public class SysDeptController extends BaseController {

    @Autowired
    SysDeptService service;

    @ApiOperation(value = "树状图-获取企业组织架构树状图")
    @GetMapping("/tree")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "是否启用 [0代表未启用的，1是查启用的]", name = "enabled", defaultValue = "0"),
            @ApiImplicitParam(value = "查询条件（按名称搜索）", name = "name")
    })
    public ServiceResponse<? extends BaseTreeVO> tree(@RequestParam(defaultValue = "0") int enabled,
                                                      @RequestParam String name) {
        return success(service.getTree(enabled, name));
    }

    @ApiOperation("添加-企业组织架构表")
    @PostMapping("/add/dept")
    public ServiceResponse<SysDept> add(@ApiParam("添加项目层级表单对象")
                                        @RequestBody SysDept sysDept) {
        //处理父路径参数值，当父值不为空，或者删除头尾为空的时候；
        if (sysDept.getParentPath() != null && ("").equals(sysDept.getParentPath().trim())) {
            sysDept.setParentPath(null);
        }
        return success(service.add(sysDept));
    }

    @ApiOperation("点击新增一级分类/新增下级分类")
    @GetMapping("/click/add/dept")
    public ServiceResponse<SysDeptVO> clickAddEqu(@ApiParam("父级ID，为空时则增加的是一级分项，非空时将在该ID下新增分项")
                                                  @RequestParam Integer parentAreaId) {
        SysDeptVO sysDeptVO = service.clickAddDept(parentAreaId);
        return success(sysDeptVO);
    }

    @ApiOperation("修改-设备设施分类")
    @PutMapping("/modify")
    public ServiceResponse<SysDept> modify(@ApiParam("企业组织架构实体")
                                                   @RequestBody SysDept entity) {
        return success(service.modify(entity));
    }

    @ApiOperation("查询-根据id")
    @GetMapping("/by/id")
    public ServiceResponse<?> getById(@ApiParam(value = "主键id", required = true)
                                      @RequestParam Long id) {
        SysDept sysDept = new SysDept();
        service.selectById(id);
        if (StringUtils.isEmpty(sysDept)) {
            return fail(ServiceException.parseInfo(ExceptionMsg.DONT_ALREADY_EXISTS));
        }
        return success(sysDept);
    }

    @ApiOperation("删除-根据id")
    @DeleteMapping("/by/id")
    public ServiceResponse<?> deleteById(@RequestParam Long id) {
        //修改delete字段的值为1
        service.updateDelete(id);
        return success();
    }



}
